﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Plugins.Core.Interfaces;

namespace Plugins.Engine
{
    [Serializable]
    public class PluginWindowManger :
        IPluginWindowManager,
        IPluginExecuteConnector<IPluginWindow>
    {
        private Dictionary<string, IPluginWindow> _windows = new Dictionary<string,IPluginWindow>();
        private Dictionary<string, IPluginWindow> _active = new Dictionary<string, IPluginWindow>();
        private bool ContainsActive(string windowName)
        {
            if (windowName != null)
            {
                return _active.ContainsKey(windowName);
            }
            return false;
        }
        private bool Contains(string windowName)
        {
            if (windowName != null)
            {
                return _windows.ContainsKey(windowName);
            }
            return false;
        }
        public Guid CreateWindowId()
        {
            return Guid.NewGuid();
        }

        [SuppressMessage("Microsoft.Naming", "CA1725")]
        public bool Attach(IPluginWindow window)
        {
            if (window != null)
            {
                if (Contains(window.Name))
                {
                    if (!ContainsActive(window.Name))
                    {
                        _active.Add(window.Name, window);
                        return true;
                    }
                }
            }
            return false;
        }

        [SuppressMessage("Microsoft.Naming", "CA1725")]
        public bool Detach(IPluginWindow window)
        {
            if (window != null)
            {
                if (Contains(window.Name))
                {
                    if (ContainsActive(window.Name))
                    {
                        _active.Remove(window.Name);
                        return true;
                    }
                }
            }
            return false;
        }
        public bool Execute(Action<IPluginWindow> action)
        {
            if (action != null)
            {
                foreach (IPluginWindow window in _active.Values)
                {
                    action(window);
                }
                return true;
            }
            return false;
        }
        #region IPluginWindowManager Members
        public bool ContainsActive(IPluginWindow window)
        {
            if (window != null)
            {
                if (ContainsActive(window.Name))
                {
                    return true;
                }
            }
            return false;
        }
        public bool Contains(IPluginWindow window)
        {
            if (window != null)
            {
                if (Contains(window.Name))
                {
                    return true;
                }
            }
            return false;
        }
        public IPluginWindow GetActiveWindow(string windowName)
        {
            if (windowName != null)
            {
                if (ContainsActive(windowName))
                {
                    return _active[windowName];
                }
            }
            return null;
        }
        public IPluginWindow GetWindow(string windowName)
        {
            if (windowName != null)
            {
                if (Contains(windowName))
                {
                    return _windows[windowName];
                }
            }
            return null;
        }
        public IEnumerable<string> ActiveWindows
        {
            get
            {
                return _active.Keys;
            }
        }
        public IEnumerable<string> Windows 
        {
            get
            {
                return _windows.Keys;
            }
        }
        public bool Register(IPluginWindow window)
        {
            if (window != null)
            {
                if (!Contains(window.Name))
                {
                    _windows.Add(window.Name, window);
                    return true;
                }
            }
            return false;
        }
        public bool Unregister(IPluginWindow window)
        {
            if (window != null)
            {
                if (Contains(window.Name))
                {
                    _windows.Remove(window.Name);
                    return true;
                }
            }
            return false;
        }
        #endregion
    }
}
